package com.cs.widgetvp.fragment;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cs.base.BaseFragment;
import com.cs.base.lib.RxTimerUtil;
import com.cs.base.util.ScreenUtil;
import com.cs.widgetvp.R;
import com.cs.widgetvp.adapter.HomeJokeVpAdapter;
import com.cs.widgetvp.adapter.HomePictureVpAdapter;
import com.cs.widgetvp.bean.JokeBean;
import com.cs.widgetvp.bean.PictureBean;
import com.cs.widgetvp.databinding.FragmentHomeBinding;
import com.cs.widgetvp.vm.HomeViewModel;

import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName: MyFragment
 * @Date: 2021/6/17
 * @Author: CS
 * @Description: 首页
 */
public class HomeFragment extends BaseFragment<HomeViewModel, FragmentHomeBinding> {
    //轮播图Adapter
    private HomePictureVpAdapter pictureVpAdapter;
    private List<String> pictureList = new ArrayList<>();

    //毒鸡汤adapter
    private HomeJokeVpAdapter jokeVpAdapter;
    private List<String> jokeList = new ArrayList<>();

    @Override
    protected FragmentHomeBinding getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return FragmentHomeBinding.inflate(inflater,container,false);
    }

    @Override
    protected HomeViewModel initViewModel() {
        return new ViewModelProvider(this).get(HomeViewModel.class);
    }

    @Override
    protected void initView() {
        initTitle();
        initPictureViewPage();
        initJokeViewPage();
    }

    @Override
    protected void initData() {
        viewModel.jokes.observe(this, new Observer<JokeBean>() {
            @Override
            public void onChanged(JokeBean jokeBean) {
                binding.tvDes.setText(jokeBean.getText());
            }
        });
        viewModel.pictures.observe(this, new Observer<PictureBean>() {
            @Override
            public void onChanged(PictureBean pictureBean) {
                pictureList.set(binding.homeIvp.getCurrentItem(),pictureBean.imgurl);
                pictureVpAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initTitle(){
        binding.homeTitleLayout.titleLSearch.setVisibility(View.VISIBLE);
        binding.homeTitleLayout.titleContent.setText("我的");
    }

    /**
     * 初始化轮播图
     */
    private void initPictureViewPage(){
        for (int i = 0; i < 3; i++) {
            pictureList.add("");
        }
        pictureVpAdapter = new HomePictureVpAdapter(getContext(),pictureList);
        binding.homeIvp.setAdapter(pictureVpAdapter);
        binding.homeIvp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < pictureVpAdapter.getItemCount(); i++) {
                    if(binding.homeIvpIndicate.getChildAt(i)!=null){
                        if(binding.homeIvpIndicate.getChildAt(i) instanceof ImageView){
                            ((ImageView) binding.homeIvpIndicate.getChildAt(i)).setImageResource(R.drawable.indicate_normal);
                        }
                    }
                }
                if(binding.homeIvpIndicate.getChildAt(position)!=null){
                    if(binding.homeIvpIndicate.getChildAt(position) instanceof ImageView){
                        ((ImageView) binding.homeIvpIndicate.getChildAt(position)).setImageResource(R.drawable.indicate_select);
                    }
                }
            }
        });
        pictureVpAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                //viewModel.getPicture();
            }
        });
        for (int i = 0; i < pictureVpAdapter.getItemCount(); i++) {
            ImageView imageView =new ImageView(getContext());
            if(i==0){
                imageView.setImageResource(R.drawable.indicate_select);
            }else {
                imageView.setImageResource(R.drawable.indicate_normal);
            }
            int p = ScreenUtil.dp2px(getContext(),5f);
            imageView.setPadding(p,p,p,p);
            binding.homeIvpIndicate.addView(imageView);
        }
        RxTimerUtil.intervalFront(this,2, new RxTimerUtil.IRxNext() {
            @Override
            public void doNext(long seconds) {
                //LogUtils.d("RxTimerUtil.interval","seconds="+seconds);
                int cur = binding.homeIvp.getCurrentItem();
                if(cur<2){
                    binding.homeIvp.setCurrentItem(cur+1);
                }else {
                    binding.homeIvp.setCurrentItem(0);
                }
            }
            @Override
            public void onFinish() {}
        });

    }

    private void initJokeViewPage(){
        jokeList.add("Android Jetpack\n" +
                "Jetpack 是一个由多个库组成的套件，可帮助开发者遵循最佳做法、减少样板代码并编写可在各种 Android 版本和设备中一致运行的代码，让开发者可将精力集中于真正重要的编码工作。");
        jokeList.add("使用 Jetpack Compose 更快地打造更出色的应用\n" +
                "Jetpack Compose 是用于构建原生 Android 界面的新工具包。它可简化并加快 Android 上的界面开发，使用更少的代码、强大的工具和直观的 Kotlin API，快速让应用生动而精彩。");
        jokeList.add("使用 Kotlin 开发 Android 应用\n" +
                "使用 Kotlin 更快地编写更出色的 Android 应用。Kotlin 是一种新型的静态类型编程语言，有超过 60% 的专业 Android 开发者在使用，它有助于提高工作效率、开发者满意度和代码安全性。");

        jokeVpAdapter=new HomeJokeVpAdapter(getContext(),jokeList);
        binding.homeJokeVp.setAdapter(jokeVpAdapter);
        binding.homeJokeVp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                if(TextUtils.isEmpty(jokeList.get(position))){
                    //viewModel.getJoke();
                }
            }
        });
    }
}